<?php
namespace app\member\controller;

use app\common\controller\MemberBase;
use app\common\model\Order as cxModel;

class Order extends MemberBase {
    public function index(){
        $cxmodel = new cxModel();
        $orlist = $cxmodel->where('uid',$this->cxbsuser['uid'])->where('class','0')->order('addtime desc')->paginate('24');
        $webs = array(
            'title' => '订单管理---'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'webs' => $webs,
            'orlist' => $orlist,
        ]);
        return view($this->temp.'order_index.htm');
    }
    //  会员中心首页
    public function get_list(){
        $getdata = request()->param();
        if(empty($getdata['f']) || $getdata['f'] == 'all'){
            $map = null;
        }elseif($getdata['f'] == 'wf'){
            $map['playstatus'] = array('in','0,4');
        }elseif($getdata['f'] == '3' || $getdata['f'] == '6'){
            $map['playstatus'] = array('in','3,6');
        }else{
            $map['playstatus'] = $getdata['f'];
        }
        $map['mid'] = $getdata['mid'];
        $cxmodel = new cxModel();
        $orlist = $cxmodel->where('uid',$this->cxbsuser['uid'])->where($map)->order('addtime desc')->paginate('24')->each(function ($item, $key){
            $item->conf = unserialize($item->conf);
            $item->comment = model('Comment')->where('oid',$item->oid)->where('uid',$this->cxbsuser['uid'])->count();
        });
        $pages = $orlist->render();
        if($map['mid'] == '100' && !empty($orlist)){
            $makemodel = new \app\common\model\OrderMake();
            foreach ($orlist as $k => $v){
                $v = $makemodel->get_one($v,false);
                $orlist[$k] = $v;
            }
        }
        $webs = array(
            'title' => '订单管理---'.$this->webdb['webname'],
            'keywords' => $this->webdb['webkeywords'],
            'description' => $this->webdb['description'],
        );
        $this->assign([
            'webs' => $webs,
            'orlist' => $orlist,
            'pages' => $pages,
            'thistime' => time(),
        ]);
        return view($this->temp.'order_list.htm');
    }
    // 查看订单详情
    public function get_cha(){
        $getdata = request()->param();
        if(empty($getdata['oid'])){
            $this->error("非法访问！");
        }
        $cxmodel = new cxModel();
        $order = $cxmodel->where('oid',$getdata['oid'])->find()->toArray();
        $order['conf'] = unserialize($order['conf']);
        $order['comment'] = model('Comment')->where('oid',$order['oid'])->where('uid',$this->cxbsuser['uid'])->count();
        $webs = array(
            'title' => "订单详情",
        );
        $this->assign([
            'order' => $order,
            'webs' => $webs,
            'thistime' => time(),
        ]);
        return view($this->temp.'order_get_cha.htm');
    }
    //  申请退款
    public function tuikuan(){
        $getdata = request()->param();
        if(empty($getdata['oid'])){
            $this->error("非法访问！");
        }
        $cxmodel = new cxModel();
        $order = $cxmodel->where('oid',$getdata['oid'])->find()->toArray();
        $order['conf'] = unserialize($order['conf']);
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            $order['playstatus'] = '6';
            $order['conf']['tuikuan'] = $data['tuikuan'] * 100;
            $order['conf']['tuikuanshuoming'] = $data['tuikuanshuoming'];
            $order['conf'] = serialize($order['conf']);
            if($cxmodel->allowField(true)->isUpdate(true)->save($order,['oid'=>$order['oid']])){
                $this->success("申请退款成功，我们将在3-5个工作日内给您处理！");
            }else{
                $this->error("申请退款失败，请重新申请");
            }
        }
        $webs = array(
            'title' => "申请退款",
        );
        $this->assign([
            'order' => $order,
            'webs' => $webs,
            'thistime' => time(),
        ]);
        return view($this->temp.'order_tuikuan.htm');
    }
    //  确认收货
    public function shouhuo(){
        $getdata = request()->param();
        if(empty($getdata['oid'])){
            $this->error("非法访问");
        }
        $cxmodel = new cxModel();
        $order = $cxmodel->where('oid',$getdata['oid'])->find()->toArray();
        $order['playstatus'] = '5';
        if($cxmodel->allowField(true)->isUpdate(true)->save($order,['oid'=>$order['oid']])){
            $this->success("确认收货成功！");
        }else{
            $this->error("确认收货失败！");
        }
    }
// 订单评价
    public function get_ping(){
        $getdata = request()->param();
        if(empty($getdata['oid'])){
            $this->error("非法访问！");
        }
        $cxmodel = new cxModel();
        $order = $cxmodel->where('oid',$getdata['oid'])->find()->toArray();
        $order['conf'] = unserialize($order['conf']);
        foreach ($order['conf']['shop'] as $k => $v){
            $num = model('Comment')->where('oid',$v['oid'])->where('aid',$v['aid'])->count();
            if($num > 0){
                continue;
            }
            $newshop[$k] = $v;
        }
        $order['conf']['shop'] = $newshop;
        $webs = array(
            'title' => "订单评价",
        );
        $this->assign([
            'order' => $order,
            'webs' => $webs,
            'thistime' => time(),
        ]);
        return view($this->temp.'order_comment.htm');
    }
}
